package com.quark.framework.utility;

import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import com.quark.data.type.QuarkPair;

public class QuarkUtilStringTools {

	public static String firstArraySplitor = ";";
	public static String secondArraySplitor = ",";
	private static Random random = new Random();
	private static Boolean randomInit = false;
	public static boolean isNullOrEmpty(String s)
	{
		return (s == null || s.isEmpty());
	}
	
	public static String getRandomString(int len)
	{
		try{
		if(!randomInit)
			random.setSeed(System.currentTimeMillis());
		randomInit = true;
		char[] tbl={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F'};
		String rtn=new String();
		for(int i=0;i<len;i++)
		{
			char ch=tbl[random.nextInt(16)];
			rtn+=ch;
		}
		return rtn;
		}catch(Exception e)
		{
			e.printStackTrace();
			return "";
		}
	}
	
	public static String escapeHtmlAttributeX(String src)
	{
		return src.replace("\"", "?quo!t").replace("'", "?apos#");
	}
	
	public static String escapeSql(String src)
	{
		return src.replace("\"", "\\\"").replace("'", "\\'");
	}
	
	public static String escapeHtmlX(String src)
	{
		return src.replace("<", "?lt#").replace(">", "?gt#").replace("'", "?apos#").replace("\"", "?quo!t").replace("  ", "&nbsp;&nbsp;").replace(",", "?dot#").replace(";","?dbd#");
	}
	
	public static String unescapeHtmlX(String src)
	{
		return src.replace("?lt#", "<").replace("?gt#", ">").replace("?apos#", "'").replace("?quo!t", "\"").replace("&nbsp;&nbsp;", "  ").replace("?dot#", ",").replace("?dbd#",";");
	}
	
	public static String escapeHtmlX2Normal(String src)
	{
		return src.replace("?lt#", "&lt;").replace("?gt#", "&gt;").replace("?apos#", "&apos;").replace("?quo!t", "&quot").replace("?dot#", ",").replace("?dbd#",";");
	}
	
	public static String escapeHtmlNormal2X(String src)
	{
		return src.replace("&lt;", "?lt#").replace("&gt;", "?gt#").replace("&apos;", "?apos#").replace("&quot", "?quo!t");
	}
	
	public static String escapeHtml(String src)
	{
		return src.replace("<", "&lt;").replace(">", "&gt;").replace("'", "&apos;").replace("\"", "&quot").replace("  ", "&nbsp;&nbsp;");
	}
	
	public static String unescapeHtml(String src)
	{
		return src.replace("&lt;", "<").replace("&gt;", ">").replace("&apos;", "'").replace("&quot", "\"").replace("&nbsp;&nbsp;", "  ");
	}
	
	public static String map2QueryString(Map<String, String> paramMap)
	{
		Iterator<String> keys = paramMap.keySet().iterator();
		String queryString = "";
		while(keys.hasNext())
		{
			String name = keys.next();
			queryString += name + "=" + paramMap.get(name);
			if(keys.hasNext())
				queryString += "&";
		}
		return queryString;
	}
	
	public static String removeQueryStringParam(String query, String param)
	{
		if(isNullOrEmpty(query))
			return "";
		String[] array = query.split("&");
		String newQuery = "";
		for(int i = 0; i < array.length; i ++)
		{
			if(!array[i].startsWith(param))
				newQuery += array[i] + "&";
			
		}
		return newQuery.substring(0, newQuery.length() - 1);
	}
	
	//-1 for strA > strB, 1 for strB > strA, 0 for equal
	public static int alphabeticalCompare(String strA, String strB)
	{
		int compareLength = strA.length() > strB.length() ? strB.length() : strA.length();
		strA = strA.toLowerCase();
		strB = strB.toLowerCase();
		for(int i = 0; i < compareLength; ++ i)
		{
				if(strA.charAt(i) > strB.charAt(i))
					return -1;
				else if(strA.charAt(i) < strB.charAt(i))
					return 1;
		}
		return 0;
	}

	//-1 for strA > strB, 1 for strB > strA, 0 for equal
	public static int numAndAlphabeticalCompare(String strA, String strB)
	{
		String strPartA = "";
		String strPartB = "";
		Integer intPartA = 0;
		Integer intPartB = 0;
		String temp = "";
		Integer pickType = 1;
		if(isNullOrEmpty(strA) && isNullOrEmpty(strB))
		{
			return 0;
		}
		else if(isNullOrEmpty(strA))
		{
			return 1;
		}
		else if(isNullOrEmpty(strB))
		{
			return -1;
		}
		for(int i = 0; i < strA.length(); i ++)
		{
			char ch = strA.charAt(i);
			if(Character.isDigit(ch))
			{
				if(pickType == 0)
				{
					temp += ch;
				}
				else
				{
					if(temp.length() > 0)
						strPartA = temp;
					temp = ch + "";
					pickType = 0;
				}
			}
			else
			{
				if(pickType == 1)
				{
					temp += ch;
				}
				else
				{
					if(temp.length() > 0)
						intPartA = Integer.valueOf(temp);
					temp = "";
					break;
				}
			}	
		}
		if(!QuarkUtilStringTools.isNullOrEmpty(temp))
		{
			if(pickType == 1)
			{
				strPartA = temp;
			}
			else
			{
				intPartA = Integer.valueOf(temp);
			}
			temp = "";
		}

		pickType = 1;
		for(int i = 0; i < strB.length(); i ++)
		{
			char ch = strB.charAt(i);
			if(Character.isDigit(ch))
			{
				if(pickType == 0)
				{
					temp += ch;
				}
				else
				{
					if(temp.length() > 0)
						strPartB = temp;
					temp = ch + "";
					pickType = 0;
				}
			}
			else
			{
				if(pickType == 1)
				{
					temp += ch;
				}
				else
				{
					if(temp.length() > 0)
						intPartB = Integer.valueOf(temp);
					temp = "";
					break;
				}
			}	
		}
		if(!QuarkUtilStringTools.isNullOrEmpty(temp))
		{
			if(pickType == 1)
			{
				strPartB = temp;
			}
			else
			{
				intPartB = Integer.valueOf(temp);
			}
			temp = "";
		}
		if(intPartA > intPartB)
			return -1;
		else if(intPartB > intPartA)
			return 1;
		else
		{
			if(QuarkUtilStringTools.isNullOrEmpty(strPartA) && !QuarkUtilStringTools.isNullOrEmpty(strPartB))
				return 1;
			if(!QuarkUtilStringTools.isNullOrEmpty(strPartA) && QuarkUtilStringTools.isNullOrEmpty(strPartB))
				return -1;
			else
			{
				int compareLength = strPartA.length() > strPartB.length() ? strPartB.length() : strPartA.length();
				strPartA = strPartA.toLowerCase();
				strPartB = strPartB.toLowerCase();
				for(int i = 0; i < compareLength; ++ i)
				{
						if(strPartA.charAt(i) > strPartB.charAt(i))
							return -1;
						else if(strPartA.charAt(i) < strPartB.charAt(i))
							return 1;
				}
			}
		}
		return 0;
	}
}
